package com.st.controller;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.ModelAndView;

import com.st.dao.IUserDao;
import com.st.entity.User;
import com.st.service.IUserService;
import com.st.util.UploadUtil;

@RequestMapping("/user/")
@Controller
public class UserController {
	
	@Autowired	
	// 从Spring容器中,根据类型查询一个Bean对象进行自动注入
	// 不需要依赖于属性的Set方法,实现对私有属性的直接注入
	private IUserService userService;
	
	@Resource
	private IUserDao userDao;
	
	/**
	 * 跳转登录界面
	 * @return
	 */
	@GetMapping("/login.do")
	public String login() {
		return "user/login";
	}
	/**
	 * 跳转修改密码页面
	 */
	@GetMapping("/modifyPwd.do")
	public String modifyPwd() {
		return "user/modifyPwd";
	}
	
	
	/**
	 * 登录操作实现
	 */
	@PostMapping("/login.do")
	public ModelAndView login(User user,
			HttpSession session,	// 保存登录状态
			ModelAndView mv
			) {
		// 参数（参数在user中）
		// 业务
		User loginUser = userService.login(user);
		
		// 结果
		if (loginUser != null) {
			session.setAttribute("user", loginUser);
			// 成功
			mv.setViewName("redirect:/index.do");// 登录成功跳转到主页
		} else {
			mv.addObject("error", "账号密码错误！");
			mv.setViewName("user/login");
		}
		
		return mv;
	}
	
	/**
	 * 修改密码
	 */
	public ModelAndView modifyPwd(String oldpassword,
			String newpassword,
			String newpassword2,
			HttpSession session,
			ModelAndView mv) {
		User loginUser = (User) session.getAttribute("user");
		int res = userService.modifyPwd(loginUser,oldpassword,newpassword,newpassword2);
		if(res > 0) {
			session.invalidate();
			//跳转到登录界面
			mv.setViewName("redirect:login.do");
		}else {
			if(res == -2) {
				mv.addObject("error", "两次密码不一致");
			}else if(res == -3) {
				mv.addObject("error", "旧密码错误");
			}else {
				mv.addObject("error", "操作失败，请联系管理员");
			}
			mv.setViewName("user/modifyPwd");
		}
		return mv;
	}
	
	
	
	/**
	 * 用户注册界面
	 */
	@GetMapping("/register.do")
	public String register() {
		return "user/register";
	}
	@PostMapping("/register.do")
	public ModelAndView register(User user,
			MultipartFile file,
			HttpServletRequest  request,
			ModelAndView mv) {
		String photo = UploadUtil.saveFile(file, request, "upload");
		user.setPhoto(photo);
		
		// 业务
		int res = userService.register(user);
		// 结果
		if(res > 0) {
			mv.setViewName("redirect:login.do");
		}else {
			if (res == -2) {
				mv.addObject("error", "用户名不可用");
			}else {
				mv.addObject("error", "数据库访问失败！");
			}
			mv.setViewName("user/register");
		}
		return mv;
	}
	
	/**
	 * 用户退出
	 */
	@GetMapping("logout.do")
	public String logout(HttpSession session) {
		session.invalidate();
		return "redirect:login.do";
	}
}
